package cn.css0209.applicationdemo190509;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.css0209.applicationdemo190509.User.UserInfoActivity;
import cn.css0209.applicationdemo190509.book.BookActivity;
import cn.css0209.applicationdemo190509.utils.OkHttp;
import cn.css0209.applicationdemo190509.utils.StatusBarUtil;

public class MenuActivity extends AppCompatActivity {
    private TextView tv_userName;
    private ImageView iv_sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        topBar();
        OkHttp okHttp = new OkHttp(this);
        okHttp.infoUser();
        tv_userName = findViewById(R.id.menu_username);
        iv_sex = findViewById(R.id.menu_sex);
        SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        String userName = sharedPreferences.getString("userName", "");
        String sex = sharedPreferences.getString("Sex", "");
        tv_userName.setText(userName);
        if(sex == null || sex.equals("other")){
            iv_sex.setImageResource(R.mipmap.sex);
        }else if(sex.equals("man")){
            iv_sex.setImageResource(R.mipmap.sex);
        } else if (sex.equals("woman")) {
            iv_sex.setImageResource(R.mipmap.woman);
        }
    }
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 1000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                System.out.println(exitTime);
            } else {
                //发送广播退出程序
                Intent intent = new Intent("cn.css0209.applicationdemo190509");
                intent.putExtra("EXITACTION", 1);
                sendBroadcast(intent);
                return super.onKeyDown(keyCode, event);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void toInfo(View v){
        Intent intent = new Intent(this, UserInfoActivity.class);
        this.startActivity(intent);
        this.finish();
    }

    public void toSetting(View view){
        Intent intent = new Intent(this, SettingActivity.class);
        this.startActivity(intent);
    }

    public void toBook(View view){
        Intent intent = new Intent(this, BookActivity.class);
        this.startActivity(intent);
    }

    public void topBar() {
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtil.setRootViewFitsSystemWindows(this, true);
        //设置状态栏透明
//        StatusBarUtil.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtil.setStatusBarDarkTheme(this, false)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }
    }
}
